using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using DIS_DAL_WS.Models;

namespace DIS_DAL_WS.DTO
{
    [DataContract]
	public class OrdersDTO
	{
        [DataMember]
		public int ID { get; set; }
        [DataMember]
		public int IDChemistWareHouse { get; set; }
        [DataMember]
        public int IDCustomer { get; set; }
        [DataMember]
		public DateTime OrderDate { get; set; }
        [DataMember]
		public bool IsVerified { get; set; }
        [DataMember]
        public bool IsShipped { get; set; }
        [DataMember]
		public bool IsPaid { get; set; }
        [DataMember]
        public ChemistWareHouseDTO ChemistWareHouse { get; set; }
        [DataMember]
        public CustomerDTO Customer{ get; set; }
        [DataMember]
        List<OrderDetailDTO> OrderDetails { get; set; }

        public static OrdersDTO Translate(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    OrderDate = Entity.OrderDate == null ? DateTime.MinValue : Entity.OrderDate.Value,
                    IsVerified = Entity.IsVerified == null ? false : Entity.IsVerified.Value,
                    IsShipped = Entity.IsShipped == null ? false : Entity.IsShipped.Value,
                    IsPaid = Entity.IsPaid == null ? false : Entity.IsPaid.Value,
                    ChemistWareHouse = ChemistWareHouseDTO.TranslateBase(Entity.ChemistWareHouse),
                    OrderDetails = new List<OrderDetailDTO>()
                };
                if (Entity.Customer != null)
                {
                    dto.Customer = CustomerDTO.Translate(Entity.Customer);
                }
                if (Entity.IDChemistWareHouse != null)
                {
                    dto.IDChemistWareHouse = Entity.IDChemistWareHouse.Value;
                }
                if (Entity.IDCustomer != null)
                {
                    dto.IDCustomer = Entity.IDCustomer.Value;
                }
                if (Entity.OrderDetail != null)
                {
                    foreach (var item in Entity.OrderDetail)
                    {
                        dto.OrderDetails.Add(OrderDetailDTO.Translate(item));
                    }
                }
                return dto;
            }
            return null;
        }

        public static OrdersDTO TranslateBase(Orders Entity)
        {
            if (Entity != null)
            {
                var dto = new OrdersDTO
                {
                    ID = Entity.ID,
                    OrderDate = Entity.OrderDate == null ? DateTime.MinValue : Entity.OrderDate.Value,
                    IsVerified = Entity.IsVerified == null ? false : Entity.IsVerified.Value,
                    IsShipped = Entity.IsShipped == null ? false : Entity.IsShipped.Value,
                    IsPaid = Entity.IsPaid == null ? false : Entity.IsPaid.Value,
                    ChemistWareHouse = ChemistWareHouseDTO.TranslateBase(Entity.ChemistWareHouse),
                };
                if (Entity.Customer != null)
                {
                    dto.Customer = CustomerDTO.Translate(Entity.Customer);
                }
                if (Entity.IDChemistWareHouse != null)
                {
                    dto.IDChemistWareHouse = Entity.IDChemistWareHouse.Value;
                }
                if (Entity.IDCustomer != null)
                {
                    dto.IDCustomer = Entity.IDCustomer.Value;
                }
                else
                {
                    dto.IDChemistWareHouse = -1;
                }
                return dto;
            }
            return null;
        }

        public static Orders Reverse(OrdersDTO obj)
        {
            var entity = new Orders
            {
                ID = obj.ID,
                OrderDate = obj.OrderDate,
                IsVerified = obj.IsVerified,
                IsShipped = obj.IsShipped,
                IsPaid = obj.IsPaid
            };
            if (obj.IDChemistWareHouse != -1 && obj.IDChemistWareHouse != 0)
            {
                entity.IDChemistWareHouse = obj.IDChemistWareHouse;
            }
            if (obj.IDCustomer != -1 && obj.IDCustomer != 0)
            {
                entity.IDCustomer = obj.IDCustomer;
            }
            return entity;
        }

        public void UpdateEntity(ref Orders entity)
        {
            entity.OrderDate = OrderDate;
            entity.IsVerified = IsVerified;
            entity.IsShipped = IsShipped;
            entity.IsPaid = IsPaid;
            if (IDChemistWareHouse != -1)
            {
                entity.IDChemistWareHouse = IDChemistWareHouse;
            }
            if (IDCustomer != -1 && IDCustomer != 0)
            {
                entity.IDCustomer = IDCustomer;
            }
        }
	}
}